﻿using MySql.Data.MySqlClient;
using server.Model;
using server.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.DAO
{
   public  class GrounpLifeDao
    {

        public string SearchLifeByGrounp(string grounpId)
        {
          
            return Utils.CollectionsConvert.ToJSON(GetGrounpByPKId(grounpId));
        }

        public Grounp_Life GetGrounpByPKId(string grounpId)
        {
            string sql = "select * from grounp_life where grounp_id = @grounp_id";
            List<Grounp_Life> result = MySqlExecuteTools.GetObjectResult<Grounp_Life>(sql,
                new MySqlParameter[] { new MySqlParameter("@grounp_id", grounpId) });
            return result[0];
        }

        public string UpdateLifeByGrounp(string grounpId, string lifeValue, string bulletCount,string hitReduceLifeValue)
        {
            string sql = "update grounp_life set lifeValue = '" + lifeValue 
                +"', bulletCount = '" + bulletCount + "', hitReduceLifeValue = '" + hitReduceLifeValue + "' where grounp_id = " + grounpId;
            MySqlExecuteTools.GetCountResult(sql);
            return "0";
        }
        public string SetGrounpLifeToUserList(string grounpId)
        {
            ///获取grounp下所有的userList
            string sql = "select ru.user_id from room r join grounp p join room_user ru on p.id = r.grounpId and r.id = ru.room_id and p.id = " + grounpId;
            List<object> userIdList = MySqlExecuteTools.GetSingleFieldResult(sql, null);

            //将GrounpLife生命值复制到life表中
            Grounp_Life grounp_life = GetGrounpByPKId(grounpId);
            userIdList.ForEach((userIdItem) => {

                int userId = (int)userIdItem;
                sql = "update life set lifeValue = '" + grounp_life.lifeValue +  "', setLifeValue = '" + grounp_life.lifeValue + 
                "', setBulletCount = '" + grounp_life.bulletCount +
                "', bulletCount = '" + grounp_life.bulletCount + "' where userId = " + userId;
                MySqlExecuteTools.GetCountResult(sql);
            });

            //清除grounp_life的值
            ResetData(grounp_life.grounp_id);
            return "0";
        }

        public void ResetData(int grounpId)
        {
            string sql = "update grounp_life set lifeValue =0, bulletCount =0  where grounp_id = " + grounpId;
            MySqlExecuteTools.GetCountResult(sql);
        }

    }
}
